package com.jd.ymcc.mq;

import com.alibaba.fastjson.JSON;
import com.jd.ymcc.domain.MediaFile;
import com.jd.ymcc.service.IMediaFileService;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Component
@RocketMQMessageListener(
        consumerGroup="consumer-video",
        topic="topic-coursevideo",
        selectorExpression="tag-push",
        messageModel= MessageModel.CLUSTERING

)
public class CourseVideoConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private IMediaFileService mediaFileService;

    @Override
    public void onMessage(MessageExt messageExt) {
        //1.将传入的文件从字节流转化为string
        String str = new String(messageExt.getBody(), StandardCharsets.UTF_8);
        //2.在将字符转化为对象
        MediaFile mediaFile = JSON.parseObject(str, MediaFile.class);
        mediaFileService.handleFile2m3u8(mediaFile);
    }
}
